using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Starter3DGameEngine
{
    public static class GameFunctions
    {

        public static Vector2 TransformtoScreenSpace(Vector2 position)
        {
            //Create a 3D vector from our 2D touch
            Vector3 point = new Vector3(position, 0f);

            // use Viewport.Unproject to tell what those two screen space positions
            // would be in world space. we'll need the projection matrix and view
            // matrix, which we have saved as member variables. We also need a world
            // matrix, which can just be identity.
            Vector3 target = GameConstants.viewport.Unproject(point,
                GameConstants.projectionMatrix, GameConstants.viewMatrix, Matrix.Identity);

            position.X = target.X;
            position.Y = target.Y;

            return position;
        }

        // CalculateCursorRay Calculates a world space ray starting at the camera's
        // "eye" and pointing in the direction of the cursor. Viewport.Unproject is used
        // to accomplish this. see the accompanying documentation for more explanation
        // of the math behind this function.
        public static Ray CalculateCursorRay(Vector2 Target)
        {
            // create 2 positions in screenspace using the cursor position. 0 is as
            // close as possible to the camera, 1 is as far away as possible.
            Vector3 nearSource = new Vector3(Target, 0f);
            Vector3 farSource = new Vector3(Target, 1f);

            // use Viewport.Unproject to tell what those two screen space positions
            // would be in world space. we'll need the projection matrix and view
            // matrix, which we have saved as member variables. We also need a world
            // matrix, which can just be identity.
            Vector3 nearPoint = GameConstants.viewport.Unproject(nearSource,
                GameConstants.projectionMatrix, GameConstants.viewMatrix, Matrix.Identity);

            Vector3 farPoint = GameConstants.viewport.Unproject(farSource,
                GameConstants.projectionMatrix, GameConstants.viewMatrix, Matrix.Identity);

            // find the direction vector that goes from the nearPoint to the farPoint
            // and normalize it....
            Vector3 direction = farPoint - nearPoint;
            direction.Normalize();

            // and then create a new ray using nearPoint as the source.
            return new Ray(nearPoint, direction);
        }


        public static void AddBullet(Bullet bullet)
        {
            bullet.isActive = true;
            //add another bullet.  Find an inactive bullet slot and use it
            //if all bullets slots are used, ignore the user input
            for (int i = 0; i < GameConstants.NumBullets; i++)
            {
                if (!GameConstants.bulletList[i].isActive)
                {
                    GameConstants.bulletList[i] = bullet;
                    AudioEngine.Shot.Play();
                    break; //exit the loop     
                }
            }
        }
    }
}
